package chabernac.android.draw;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.graphics.Canvas;
import android.view.View;

public class DrawableComposite extends AbstractDrawable {
	private final List<AbstractDrawable> myDrawables = new ArrayList<AbstractDrawable>(100);

	public void clear() {
		myDrawables.clear();
	}

	public void addDrawable(AbstractDrawable aDrawable) {
		if (aDrawable != null)
			myDrawables.add(aDrawable);
	}

	public void sort() {
		Collections.sort(myDrawables, new LayerComparator());
	}

	private List<AbstractDrawable> getDrawables() {
		return new ArrayList<AbstractDrawable>(myDrawables);
	}

	@Override
	public void paint(Canvas aCanvas, View aView) {
		// Log.d("DrawableComposite", "Start painting composite");
		// long t11 = System.currentTimeMillis();
		for (AbstractDrawable theDrawable : getDrawables()) {
			// long t1 = System.currentTimeMillis();
			if (theDrawable != null)
				theDrawable.paint(aCanvas, aView);
			// long t2 = System.currentTimeMillis();
			// long delta = t2 - t1;
			// if(delta > 50){
			// Log.d("DrawableComposite", "Painting '" +
			// theDrawable.getClass().getName() + " took " + delta + " ms");
			// }
		}
		// long t12 = System.currentTimeMillis();
		// Log.d("DrawableComposite", "Painting compostie took '" + (t12 - t11) +
		// "' ms");
	}
}
